package com.hntech.base.io.bio;

import com.hntech.util.IOUtil;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Autho changqi.wu
 * @Date 路在脚下，使劲踩！
 */
public class BioServer {

    public static int port = 8000;
    public static String address = "127.0.0.1";

    public static void main(String[] args) throws IOException {

        ServerSocket server = new ServerSocket(port);

        int count = 0;

        System.out.println("start server " + port);

        while (true) {
            Socket socket = server.accept();
            InputStream inputstream = socket.getInputStream();

            System.out.println("request : " + IOUtil.readInputStream(inputstream));  //IO流会堵塞，获取不到任何信息
            OutputStream outputStream = socket.getOutputStream();
//        outputStream.write(IOUtil.stringToByte("response " + count++));
//        outputStream.flush();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
            bw.write("response " + count++);
            bw.flush();

        }
    }


}
